package com.lqd.mall.system.repository;

import com.lqd.mall.jpa.repository.BaseRepository;
import com.lqd.mall.interaction.system.SysMenuEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Set;

/**
 * @author 零起点
 * @create 2022-02-20 15:39
 */
public interface SysMenuRepository extends BaseRepository< SysMenuEntity,Long>, JpaRepository<SysMenuEntity,Long>,JpaSpecificationExecutor<SysMenuEntity> {
    @Query(value = " select distinct m.perms" +
            " from sys_menu m" +
            "  left join sys_role_menu rm on m.menu_id = rm.menu_id" +
            "  left join sys_user_role ur on rm.role_id = ur.role_id" +
            "  left join sys_role r on r.role_id = ur.role_id" +
            " where m.status = '0' and r.status = '0' and ur.user_id = :userId",nativeQuery = true)
    Set<String> selectMenuPermsByUserId(Long userId);


    @Query(value = "select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.`query`, m.visible, " +
            "m.status, ifnull(m.perms,'') as perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time,m." +
            "create_by , m.remark,m.upate_time,m.upate_by,m.del_flag" +
            " from sys_menu m" +
            " left join sys_role_menu rm on m.menu_id = rm.menu_id" +
            " left join sys_user_role ur on rm.role_id = ur.role_id" +
            " left join sys_role ro on ur.role_id = ro.role_id" +
            " left join sys_user u on ur.user_id = u.user_id" +
            " where u.user_id = :userId and m.menu_type in ('M', 'C') and m.status = 0  AND ro.status = 0" +
            " order by m.parent_id, m.order_num",nativeQuery = true)
    List<SysMenuEntity> selectMenuTreeByUserId(Long userId);
}
